package kau.common;

import java.io.IOException;
import java.io.Reader;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class DBUtil {

	private static DBUtil instance;
	private String resource = "dbConfig/SqlMapConfig.xml";
	private Reader reader = null;
	SqlSessionFactoryBuilder builder = null;
	SqlSessionFactory factory = null;

	private DBUtil() {
		try {
			// SqlMapConfig parsing
			reader = Resources.getResourceAsReader(resource);
			builder = new SqlSessionFactoryBuilder();
			factory = builder.build(reader);
		} catch (IOException e) {
			System.out.println("config file read 실패!! " + e);
		}
	}

	public static DBUtil getInstance() {
		if (instance == null) {
			// System.out.println("getInstance!!");
			instance = new DBUtil();
		}
		return instance;
	}

	public SqlSession getSession()
	{
		return factory.openSession(); 
	}
}